package us.congcong.jhostsmanager.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.Optional;

import org.apache.commons.lang3.SystemUtils;

import javafx.scene.control.Alert;
import javafx.scene.control.TextInputDialog;
import us.congcong.jhostsmanager.HostsEditor;

public class HostsUtil {
	//sudo password
	private static String sudoPwd = null;
	
	public static String getHostsContent(){
		String fileName = null;
		String fileContent = "";
		
		if(SystemUtils.IS_OS_LINUX || SystemUtils.IS_OS_MAC || SystemUtils.IS_OS_UNIX){
			fileName = "/etc/hosts";
		} else if(SystemUtils.IS_OS_WINDOWS){
			fileName = "C:\\Windows\\System32\\drivers\\etc\\hosts";
		} else {
			return "未知系统";
		}
		
		try {
			File file = new File(fileName);
			fileContent = org.apache.commons.io.FileUtils.readFileToString(file, "UTF-8");
		} catch (IOException e) {
		 e.printStackTrace();
		}
		return fileContent;
    }
    
	public static Boolean saveHostsContent(String fileContent){
		File file = HostsUtil.getHostsFile();
		
		if(file == null){
			return false;
		} else {
			try {
				if(SystemUtils.IS_OS_LINUX || SystemUtils.IS_OS_MAC || SystemUtils.IS_OS_UNIX){
					if(sudoPwd == null){
						TextInputDialog dialog = new TextInputDialog("");
						dialog.setTitle("Sudo Dialog");
						dialog.setHeaderText("Look, a Sudo Input Dialog");
						dialog.setContentText("Please enter your sudo pwd:");

						Optional<String> result = dialog.showAndWait();
						if (result.isPresent()){
							
						}

						result.ifPresent(pwd -> sudoPwd = pwd);
						
						String[] cmds = {"/bin/bash", "-c", "echo \"" + sudoPwd + "\" | " + "sudo" + " -S " + " chmod 777 " + file};
						Process process = Runtime.getRuntime().exec(cmds);
						
						BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(process.getErrorStream()));
						String line;
						Boolean isSucc = true;
						while((line=bufferedReader.readLine())!=null)
						{
							if(line.contains("try again")){
								isSucc = false;
							}
						}
						
						if(!isSucc){
							sudoPwd = null;
							
							Alert alert = new Alert(Alert.AlertType.ERROR);
							alert.setTitle("Wrong Password");
							alert.setHeaderText("Wrong Password");
							alert.setContentText("Please try agian");
							alert.showAndWait();
							
							return false;
						}
					}
				}
				
				org.apache.commons.io.FileUtils.writeStringToFile(file, fileContent, "UTF-8");
			} catch (IOException e) {
				e.printStackTrace();
			}
			return true;
		}
    }
    
	public static File getHostsFile(){
    	String fileName = null;
    	if(SystemUtils.IS_OS_LINUX || SystemUtils.IS_OS_MAC){
			fileName = "/etc/hosts";
			return new File(fileName);
		} else if(SystemUtils.IS_OS_WINDOWS){
			fileName = System.getenv("windir") + "\\System32\\drivers\\etc\\hosts";
			return new File(fileName);
		} else {
			System.out.println("未知系统");
			return null;
		}
    }
}
